package com.huang.crawler.crawler.img;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.huang.crawler.constant.UrlConstant;
import com.huang.crawler.util.CommonUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * 
 * @ClassName:BaiduImgCrawler.java
 * @ClassDescribe:百度图片爬虫
 * @createPerson:huangying
 * @createDate:2018年7月26日 下午1:40:05
 * @version
 */
public class BaiduImgCrawler {

	/**
	 * 
	 * @ClassDescribe:总入口
	 * @auth:huangying
	 * @createDate:2018年7月26日 下午3:10:24
	 * @param args
	 */
	public static void main(String[] args) {
		BaiduImgCrawler crawler = new BaiduImgCrawler();
		crawler.crawlStart("波多野结衣");// 输入你想查询的关键字
	}

	/**
	 * 
	 * @ClassDescribe:爬虫启动
	 * @auth:huangying
	 * @createDate:2018年7月26日 下午3:01:46
	 * @param keyword
	 *            关键字
	 */
	public void crawlStart(String keyword) {
		/**
		 * 链接容器
		 */
		List<String> imgList = new ArrayList<String>();
		try {
			for (int page = 1; page <= 100; page++) {
				String url = UrlConstant.BAIDU_IMG_AJAX_URL.replace("<keyword>", URLEncoder.encode(keyword, "UTF-8"))
						.replace("<page>", String.valueOf(page * 30));
				CloseableHttpClient client = HttpClients.createDefault();
				HttpGet get = new HttpGet(url);
				CloseableHttpResponse response = client.execute(get);
				imgList.addAll(parse(response));
			}
			CommonUtils.downloadPicture(imgList, keyword);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @ClassDescribe:解析数据获取img链接
	 * @auth:huangying
	 * @createDate:2018年7月26日 下午3:02:03
	 * @param response
	 *            http响应
	 * @throws UnsupportedOperationException
	 * @throws IOException
	 */
	public List<String> parse(CloseableHttpResponse response) throws UnsupportedOperationException, IOException {
		/**
		 * 链接容器
		 */
		List<String> imgList = new ArrayList<String>();
		String resultStr = CommonUtils.readInputStream(response.getEntity().getContent());
		JSONObject jsonObject = JSONObject.fromObject(resultStr);
		JSONArray dataJsonArr = jsonObject.getJSONArray("data");
		for (int i = 0; i < dataJsonArr.size(); i++) {
			JSONObject dataJson = (JSONObject) dataJsonArr.get(i);
			try {
				JSONArray replaceUrl = dataJson.getJSONArray("replaceUrl");
				if (replaceUrl.size() > 1) {
					JSONObject urlJson = (JSONObject) replaceUrl.get(1);
					imgList.add(urlJson.getString("ObjURL"));
				} else {
					JSONObject urlJson = (JSONObject) replaceUrl.get(0);
					imgList.add(urlJson.getString("ObjURL"));
				}
			} catch (Exception e) {
				return imgList;
			}
		}
		return imgList;
	}
}